﻿// Custom example logic
var curDirId = 0;
var curDirName = "";
var gSt = 3;
var uploader;
var gFileArr = new Array();
var gInterval;
var gReturnServerData = "";
var gShowEditId = 0;
function saveGroup()
{
	var gn = $("#txtGroupName").val();

	if(gn!="" || gReturnServerData!="")
	{
		if(gn!=$("#txtGroupName").attr("data"))
		{
			if(!confirm("确定要修改主题吗？"))
			{
				return;
			}
		}
		$.post("?act=filegroup&op=ajaxSaveFile",{fileData:gReturnServerData,group:gn,oldGroup:$("#txtGroupName").attr("data"),id:gId},function(data){
			
			//alert("333333"+data);
	    	 var j = eval("("+data+")");
			 if(j.st=="ok")
			 {
				 $( "#dialog-form" ).dialog("close");
				 gReturnServerData = "";
			 }
			 else { alert(j.msg);}
		});
	}
	else
	{
		if(gId==0)
		{
			alert("请选择图片并输入链接主题");
		}
		else
		{
			alert("保存成功");
		}
		
	}
}
function initUploader()
{
	uploader = new plupload.Uploader({
	runtimes : 'html5,flash,silverlight,html4',
	browse_button : 'pickfiles', // you can pass in id...
	container: document.getElementById('dialog-form'), // ... or DOM Element itself
	url : 'upload.php',
	flash_swf_url : 'js/Moxie.swf',
	silverlight_xap_url : 'js/Moxie.xap',
	chunk_size:"1mb",
	filters : {
		max_file_size : '5000mb',
		mime_types: [
			{title : "Image files", extensions : "jpg,gif,png"}
		]
	},

	init: {
		PostInit: function() {
			document.getElementById('filelist').innerHTML = ''; 
		},

		FilesAdded: function(up, files) {
			plupload.each(files, function(file) {
				//initProgrssBar();
				var html = $("#divTempFileHtml").html();
				html = html.replace("TBLID","tblFile_"+file.id);
				var str = file.name;
				if(str.length>30)
				{
					str = str.substring(0,30)+"...";
				}
				html = html.replace("NAME",str);
				html = html.replace("SIZE",plupload.formatSize(file.size));
				document.getElementById('filelist').innerHTML += "<li>"+html+"</li>";
				//alert(html);
				$("#tblFile_"+file.id).find("#progressbar").progressbar({value:0});
				if($("#filelist").is(":hidden"))
				{
					$("#filelist").show();
				}
				uploader.start();
 
			});
		},

		UploadProgress: function(up, file) {
			//document.getElementById("tdProess_"+file.id).innerHTML = '<span>' + file.percent + "%</span>";
			$("#tblFile_"+file.id).find("#progressbar").progressbar({value:file.percent});
			//$("#tblFile_"+file.id).find("#progressbar").progressbar("value",file.percent);
			//document.getElementById(file.id).getElementsByTagName('b')[0].innerHTML = '<span>' + file.percent + "%</span>";
		},
        FileUploaded:function(up,file,o)
		{
			//alert(o);
			$("#dialog-confirm").dialog("open");
			var json = eval("("+o.response+")");
			$("#tblFile_"+file.id).find("#imgOk").show();
			gFileArr[file.id] = false;
			$.post("?act=filegroup&op=saveFile",{name:file.name,path:json.path,dirId:curDirId,dirName:curDirName,fid:file.id},function(data){
				try
				{
				var j= eval("("+data+")");
				if(j.st=="ok")
				{
					$("#imgUpload").show();
					$("#imgUpload")[0].src=j.path;
					gReturnServerData = j.data;
					gFileArr[j.fid] = true;
					refreshUpload();
				}
				}
				catch(e){}
				});
			gInterval = setInterval("checkFinish()",1000);
		},
		UploadComplete:function(up,o)
		{
			//alert("全部上传完毕");
			
		},
		Error: function(up, err) {
			document.getElementById('console').innerHTML += "\nError #" + err.code + ": " + err.message;
		}
	}
});
	uploader.init();
}
function checkFinish()
{
	var haveAllTure = true;
	for(var item in gFileArr)
	{
	    if(!gFileArr[item])
		{
			haveAllTure = false;
			break;
		}
	}
	if(haveAllTure)
	{
		clearInterval(gInterval);
		//alert("上传完毕");
		
		$("#dialog-confirm").find("#msg").html("处理完毕");
		$("#dialog-confirm").dialog("close");
		//alert("上传完毕");
		//headerShowTips("感谢您的上传 继续上传文件吗？",1);
	}
}
function initProgrssBar(jo)
{
	var progressbar = jo;
	progressbar.progressbar({
      value: 0
    });
	 
}
$(function() {
	//newUploader("tdShowImg");
 
	    setHeaderPos("素材主题");
		$( "input[type=submit], button" )
			.button()
			.click(function(event) {
			    var bid = $(event.target)[0].id;
				if(bid=="")
				{
					bid = $(event.target).parent()[0].id;
				}
	           // alert(bid);
			    $("#name").val("");
                if(bid=="createUser")
			    {
                    gId  = 0;
					$("#imgUpload")[0].src="about:blank";
					$("#imgUpload").hide();
					$("#txtGroupName").val("");
					$("#idSubjectTips").hide();
					if(refreshUpload())
					{
						$(".ui-dialog-title").html($(".ui-dialog-title").attr("deftitle"));
						$("#dialog-form").dialog("open");
					}
			    }
				else if(bid=="btnDelete")
				{
					doDelFile();
				}
				else if(bid=="btnModify")
				{
					toModifyPage();
				}
				else if(bid=="btnDetailSave")
			    {
					//alert($("#fpdid").val());
					if($("#id").val().indexOf(",")!=-1 || $("#pdid").val()!="0")
					{
						if(confirm("您已选择多个文件，如果继续，则会覆盖所选文件已有的信息，是否继续？"))
						{
							$("#optype").val("save");
							$("#form").submit();
						}
					}
					else
					{
						$("#optype").val("save");
						$("#form").submit();
					}
				}
				else if(bid=="btnDetailPost")
				{
					if($("#id").val().indexOf(",")!=-1 || $("#pdid").val()!="0")
					{
						if(confirm("您已选择多个文件，如果继续，则会覆盖所选文件已有的信息，是否继续？"))
						{
							$("#optype").val("post");
							$("#form").submit();
						}
					}
					else
					{
						$("#optype").val("post");
						$("#form").submit();
					}
				}
				else if(bid=="btnDetailUnPost")
				{
					if($("#id").val().indexOf(",")!=-1 || $("#pdid").val()!="0")
					{
						if(confirm("您已选择多个文件，如果继续，则会下架所选文件，是否继续？"))
						{
							$.get("?act="+$("#act").val()+"&op=saveInfo&isPost=-1&id="+$("#id").val()+"&did="+$("#pdid").val()+"&src=ajax",function(data){
								alert("下架成功");
							});
						}
					}
					else
					{
						$.get("?act="+$("#act").val()+"&op=saveInfo&isPost=-1&id="+$("#id").val()+"&src=ajax",function(data){
						alert("下架成功");
						});
					}
				}
				else if(bid=="btnUnPost")
				{
					doUnPost();
				}
				else if(bid=="btnPost")
				{
					//doPost();
					toModifyPage();
				}
				else if(bid=="btnShowDirForm")
				{
					if($("#id").val().indexOf(",")!=-1 || $("#fpdid").val()!="0")
					{
						if(confirm("您已选择多个文件，如果继续，则会删除所选文件已所属的目录，是否继续？"))
						{
							$("#dialog-form-adddir").dialog("open");
							$.post("?act=upload&op=removeAllFileDirType&id="+$("#fid").val()+"&pdid="+$("#fpdid").val(),function(d){
								var j = eval("("+d+")");
								gId = j.id;
							});
							
						}
					}
					else
					{
						$("#dialog-form-adddir").dialog("open");
					}
				}
				else if(bid=="btnAddApply")
				{
					 doAddApplyList();
				}
				return false;
				//alert("[pppp");
		});
		
		$( "#dialog-confirm" ).dialog({
			autoOpen: false,
			height:200,
			width: 300,
			modal: true,
			close: function() {
				  return false;
			},
			open:function(event, ui){
				$(".ui-dialog-titlebar-close", $(this).parent()).hide();
				}
		});
		$( "#dialog-form" ).dialog({
			autoOpen: false,
			height:450,
			width: 500,
			modal: true,
			buttons: {
				"保存": function() {
 					 saveGroup();
				},
				"关闭": function() {
 					$( "#dialog-form" ).dialog("close");
				}

			},
			close: function() {
				 
				 getPage("?act=filegroup&op=getLstFile&group=1&st="+gSt);
			}
		});
 
		getPage("?act=filegroup&op=getLstFile&group=1&st=3");
		
		$("#chkSelAll").change(function(){
			 if(this.checked)
			 {
				 $("#ulList").find("input").each(function(){
					 if(this.disabled==false)
					 {
					 	this.checked = true;
					 }
					 });
			 }
			 else
			 {
				 $("#ulList").find("input").each(function(){
					 this.checked = false;
					 });
			 }
			});
			
		$("#chkDirAll").change(function(){
			 if(this.checked)
			 {
				 $("#ulDir").find("input").each(function(){
					 if(this.disabled==false)
					 {
					 	this.checked = true;
					 }
					 });
			 }
			 else
			 {
				 $("#ulDir").find("input").each(function(){
					 this.checked = false;
					 });
			 }
			});			
	 
	   //$("#btnAddApply").css("background-color","#888");
		$("#selSt").change(function(){showListBySt(this)});
		initUploader();
		$("#btnUnPost").css("background","#888");
		gShowEditId = getQueryString("showEdit");
		if(gShowEditId!=null)
		{
			$("#selSt").val("1");
			$("#selSt").change();
		}
});
function refreshUpload()
{
					$("#editId").val("");
			        //$("#ui-id-1").html("添加超大附件");
					$("#filelist").hide();
					document.getElementById('filelist').innerHTML ="";
 					uploader.refresh();
					return true;
}
function doAddApplyList()
{
	 if(gSt!=3)
	 {
		return;
	 }
	 var id = "";
	 $("#ulList").find("input").each(function(){
		 if(this.checked)
		 {
			 id = id+this.value+",";
			 gId = this.value;
		 }
		 });
     
     if(id=="")
	 {
		 alert("请选择要加入申请的主题");
		 return;
		 if(confirm("您没有选择文件，是否要把当前目录 "+curDirName+" 已完善信息所有文件加入到待审批列表？"))
		 {
			 gid = 0;
			 $("#id").val("0");
			 $("#pdid").val(curDirId);
			 $("#fpdid").val(curDirId);
		 }
		 else
		 {
			 return;
		 }
	 }
	 gId = id.substring(0,id.length-1);
	 $.post("?act=filegroup&op=doAddApplyList",{id:gId,dirId:curDirId},function(d){
		 $("#pdid").val("0");
		 var j = eval("("+d+")");
		 if(j.st=="ok")
		 {
			// alert("文件已加入待审批列表 请查阅");
			headerShowTips("文件已加入审批列表，是否选择文件发送客户审批？",3)
		 }
		 else
		 {
			 alert("加入失败");
		 }
		 
	});
}
function showListBySt(obj)
{
			$("#btnAddApply").css("background","#888");
			$("#btnUnPost").css("background","#888");
			gSt = obj.value;
			if(gSt==0)
			{
				//$("#btnDelete").show();
				//$("#btnPost").show();
				//$("#btnUnPost").hide();
		 
			}
			else if(gSt==1)
			{
				//$("#btnUnPost").show();
				//$("#btnPost").hide();
				//$("#btnDelete").hide();
				$("#btnUnPost").css("background","#597DDB");
			}
            else if(gSt==3)
			{
				$("#btnAddApply").css("background","#597DDB");
			}

			else if(gSt==2)
			{
				//$("#btnPost").hide();
				//$("#btnDelete").hide();
				//$("#btnUnPost").hide();
			}
			getPage("?act=filegroup&op=getLstFile&group=1&st="+gSt);
		 
}
function doPost()
{
	 var id = "";
	 $("#ulList").find("input").each(function(){
		 if(this.checked)
		 {
			 id = id+this.value+",";
		 }
		 });
     if(id=="")
	 {
		 if(!confirm("您没有选择文件，是否要完善当前目录 "+curDirName+" 所有文件的信息？"))
		 {
			 return;
		 }

	 }
	 id = id.substring(0,id.length-1);
	$.post("?act=upload&op=saveInfo&src=ajax",{id:id,optype:"post"},function(d){
		 var j = eval("("+d+")");
		 if(j.st=="ok")
		 {
			 alert("发布成功");
			 getPage("?act=ajax&op=getLstFile&did="+curDirId+"&st="+gSt);
		 }
	 });
}
function doUnPost()
{
	 var id = "";
	 $("#ulList").find("input").each(function(){
		 if(this.checked)
		 {
			 id = id+this.value+",";
		 }
		 });
     if(id=="")
	 {
		 alert("请选择要下架的文件");return;
	 }
	 if(confirm("确定要下架选中的文件吗？"))
	 {
	 id = id.substring(0,id.length-1);
 
 
	 $.post("?act=upload&op=saveInfo&src=ajax",{id:id,isPost:"-1"},function(d){
		 var j = eval("("+d+")");
		 if(j.st=="ok")
		 {
			 alert("下架成功");
			 getPage("?act=filegroup&op=getLstFile&group=1&st="+gSt);
		 }
	 });
	 }
	
}
function removeDir()
{
	 var id = "";
	 $("#ulList").find("input").each(function(){
		 if(this.checked)
		 {
			 id = id+this.value+",";
		 }
	 });
     if(id=="")
	 {
		 alert("请选择要从当前目录 "+curDirName+" 移出的文件");return;
	 }
 	if(confirm("确定要把所选文件从目录 "+curDirName+" 移出吗？"))
	 {
	 id = id.substring(0,id.length-1);
	 $.post("?act=upload&op=ajaxRemoveFile",{id:id,did:curDirId},function(d){
		 var j = eval("("+d+")");
		 if(j.st=="ok")
		 {
			 alert("操作成功");
			 getPage("?act=ajax&op=getLstFile&did="+curDirId+"&st="+gSt);
		 }
	 });
	 }
}
function setDir()
{
	 var did = "";
	 $("#ulDir").find("input").each(function(){
		 if(this.checked)
		 {
			 did = did+this.value+",";
		 }
		 });
     if(did=="")
	 {
		 alert("请选择要存放的目录");return;
	 }
	 var id = "";
	 $("#ulList").find("input").each(function(){
		 if(this.checked)
		 {
			 id = id+this.value+",";
		 }
		 });
     if(id=="")
	 {
		 alert("请选择要存放的文件");return;
	 }
	 if(confirm("确定执行此操作吗？"))
	 {
	 id = id.substring(0,id.length-1);
	 did = did.substring(0,did.length-1);
	 $.post("?act=upload&op=ajaxDirFile",{id:id,did:did},function(d){
		 var j = eval("("+d+")");
		 if(j.st=="ok")
		 {
			 alert("操作成功");
			// getPage("?act=ajax&op=getLstFile&did="+curDirId+"&st="+gSt);
		 }
	 });
	 }
}
function doDelFile()
{
	 var id = "";
	 $("#ulList").find("input").each(function(){
		 if(this.checked)
		 {
			 id = id+this.value+",";
		 }
		 });
     if(id=="")
	 {
		 alert("请选择要删除的文件");return;
	 }
	 if(confirm("确定要删除选中的文件吗？"))
	 {
	 id = id.substring(0,id.length-1);
	 $.post("?act=upload&op=ajaxDel",{id:id},function(d){
		 var j = eval("("+d+")");
		 if(j.st=="ok")
		 {
			 alert("成功删除");
			 getPage("?act=ajax&op=getLstFile&did="+curDirId+"&st="+gSt);
		 }
	 });
	 }
}
 
function saveFileType()
{
   $("#fromFyleType")[0].submit();
}

function showFile(o,id,name)
{
    $("#chkSelAll")[0].checked = false;
	$("#ulDir").find("li").each(function(){
	   $(this).css("background-color","#FFFFFF");
	});
	$(o).parent().css("background-color","#EEE");
	if(id=="0")
	{
		$("#title").html('全部目录');
		//$("#selSt").change(function(){showListBySt(this)});
		//$("#btnSetDir").show();
		//$("#btnRemoveDir").hide();
		//$("#createUser").show();
		//$("#btnModify").show();
		//$("#btnDelete").hide();
		$("#btnPost").hide();
	}
	else
	{
		//$("#btnDelete").show();
		$("#btnPost").show();
		//$("#btnPost").hide();
		//$("#btnModify").hide();
		//$("#btnDelete").hide();
		//$("#createUser").hide();
		//$("#btnSetDir").hide();
		//$("#btnRemoveDir").show();
		$("#title").html("目录："+name);
	}
	curDirId = id;
	curDirName = name;
	//showListBySt();
	$("#selSt").val(0);
	$("#selSt").change();
	//getPage("?act=ajax&op=getLstFile&did="+curDirId+"&st="+gSt);
}
 
function getPage(url)
{
		$.get(url+"&src=edit",function(data){
		var j = eval("("+data+")");
		if(j.st=="ok")
		{
			$("#ulList").html("");
			if(j.data.total>0)
			{
				var ul = "";
				for(i=0;i<j.data.data.length;i++)
				{
					var name = j.data.data[i].name;
					
					var name1 = name;
					var name2 = "";
					if(name.indexOf("<br/>")!=-1)
					{
						var names = name.split("<br/>");
					 
						if(names[0].length>8)
						{
							name1 = names[0].substring(0,8)+"..";
						}
					    else
						{
							name1 = names[0];
						}
						if(names[1].length>10)
						{
							name2 = names[1].substring(0,10)+"..";
						}
						else
						{
							name2 = names[1];
						}
					}
					else
					{
						if(name1.length>8)
						{
							name1 = name1.substring(0,8)+"..";
						}
					}
					 
					ul = ul+'<li><img src="'+j.data.data[i].pic+'" onClick="showDetail('+j.data.data[i].id+');"/><div title="'+names[0]+'">'+name1+'<br/>'+name2+'</div><input type="checkbox" id="chk" value="'+j.data.data[i].id+'"/></li>';
				}
				$("#ulList").append(ul);
				if(gShowEditId!=0 && gShowEditId!=null)
				{
					showDetail(gShowEditId);
					gShowEditId = 0;
				}
			}
 
			$("#divPageNav").html(j.data.pageNav);
			$("#total").html(j.data.total);
		}
		});
}
function showDataPage()
{
	setHeaderPos("素材上传");
	$("#tblDetail").hide();
	$("#tblData").show();
	$("#fid").val("0");
	$("#id").val("0");
	$("#pdid").val("0");
	$("#fpdid").val("0");
	//$("#selSt").val(0);
	//$("#selSt").change();
	getPage("?act=ajax&op=getLstFile&did="+curDirId+"&st="+gSt);
}
//////////////////////////////
var gId = 0;
function toModifyPage()
{
	 var id = "";
	 $("#ulList").find("input").each(function(){
		 if(this.checked)
		 {
			 id = id+this.value+",";
		 }
		 });
      
     if(id=="")
	 {
		 if(confirm("您没有选择文件，是否要完善当前目录 "+curDirName+" 所有文件的信息？"))
		 {
			 gid = 0;
			 $("#id").val("0");
			 $("#pdid").val(curDirId);
			 $("#fpdid").val(curDirId);
		 }
		 else
		 {
			 return;
		 }
	 }
	 //alert(id);
	 gId = id.substring(0,id.length-1);
	 //alert(gIds);
	  
	 showDetail(gId);
	 
}
function showDetail(id)
{
 	gId = id;
	$(".ui-dialog-title").html("编辑主题");
	$("#dialog-form").dialog("open");
	$.get("?act=ajax&op=getFileEditInfo&isSub=1&id="+id,function(data){
		 var j = eval("("+data+")");
		if(j.st=="ok")
		{
			$("#imgUpload").show();
			$("#imgUpload")[0].src=j.data.path;
			$("#txtGroupName").val(j.data.mark);
			$("#txtGroupName").attr("data",j.data.zhuanTi);
		}
	});
}
var garrt=new Array(),garrc=new Array();
function getDetailInfo()
{
		$.get("?act=ajax&op=getFileEditInfo&id="+gId,function(data){
		 var j = eval("("+data+")");
		if(j.st=="ok")
		{
	        if(typeof(j.data.path)!="undefined" && j.data.path!=null)
			{
				$("#tdShowImg").css("background-image","url("+j.data.path.replace("s.","sb.")+")");
				$("#tdShowImg").css("background-position","center");
				$("#tdShowImg").css("background-repeat","no-repeat");
			}
			//$("#tdShowImg").css("background-size","contain");
			$("#author").val(j.data.author);
			//alert($("#name").length());
			$("#mark").val(j.data.mark);
		    //alert($("#pdid").val());
			//alert( $("#id").val());
			if(j.data.isPic==1)
			{
				$("#btnUploadPrevPic").hide();
			}
			else
			{
				$("#btnUploadPrevPic").show();
			}
				$("#idid").html(gId);
				var showHeader = j.data.mark;
				if(showHeader==null)
				{
					showHeader = j.data.name;
				}
				setHeaderPos(showHeader+" &nbsp;&nbsp;<a href='javascript:showDataPage();'>返回</a>");
			
			$("#instro").val(j.data.instro);
			$("#uploader").html(j.data.uname);
			$("#zhuanTi").val(j.data.zhuanTi);
			$("#limitUse").val(j.data.limitUse);
			$("#size").append(j.data.size);
			$("#keywords").val(j.data.keywords);
			$("#avaliDate").val(j.data.avaliDate);
			$("#info").html(j.data.fileTypeName);
			$("#brand").attr("sel",j.data.brandId);
			$("#car").attr("sel",j.data.carId);
			$("#type").attr("sel",j.data.typeId);
			$("#style").attr("sel",j.data.styleId);
			if(j.data.year==null)
			{
				$("#year").attr("sel","0");
			}
			else
			{
			$("#year").attr("sel",j.data.year);
			}
			//$("#brandName").attr("sel",j.data.brandName);
			//$("#carName").attr("sel",j.data.carName);
			//$("#typeName").attr("sel",j.data.typeName);
			//$("#styleName").attr("sel",j.data.styleName);
			
			$("#tblSelCar").html("");
			$("#tblSelType").html("");
			$("#dirTypes").html("—内容类型&nbsp;(0)");
			$("#dirCars").html("—品牌车型&nbsp;(0)");
			//alert(j.data.isPost);
			if(j.data.isPost==0)
			{
				$("#btnDetailSave").show();
				//$("#btnDetailPost").show();
				$("#btnDetailUnPost").hide();
			}
			else if(j.data.isPost==1)
			{
				$("#btnDetailSave").show();
				$("#btnDetailPost").hide();
				$("#btnDetailUnPost").show();
			}
			else if(j.data.isPost==2)
			{
				$("#btnDetailPost").hide();
				$("#btnDetailSave").hide();
				$("#btnDetailUnPost").hide();
			}
 
			try
			{
            if(j.data.typeNames!=null)
			{
				garrt = j.data.typeNames.split(",");
				
			}
			if(j.data.carNames!=null)
			{
				garrc = j.data.carNames.split(",");
			}
			//alert("cccc");
			showFileTypes(garrt,garrc);
			}catch(e){}
			initDetailPage();
		}
		else
		{
			//window.location.href="index.html";
		}
		 	 
		 });
}
function showFileTypes(arrt,arrc)
{
	$("#tblSelType").html("");
	$("#dirTypes").html("—内容类型&nbsp;(0)");
	if(arrt.length>0)
	{
		
		var jx = 0;
		for(i=0;i<arrt.length;i++)
		{
			var t = arrt[i];
			if(t.indexOf(":")!=-1)
			{
			var n = t.split(":");
			
			$("#tblSelType").append('<tr><td style="padding:0; border:none;width:200px; padding-left:20px;">'+n[0]+' '+ n[1]+'</td><td style="font-size:20px;padding:0; border:none; cursor:pointer" onclick="delFileType(1,'+i+')">x</td></tr>');
			jx++;
			}
		}
		$("#dirTypes").html("—内容类型&nbsp;("+jx+")");
	}
	$("#tblSelCar").html("");
	$("#dirCars").html("—品牌车型&nbsp;(0)");
	if(arrc.length>0)
	{
		
		var j = 0;
		for(i=0;i<arrc.length;i++)
		{
			var t = arrc[i];
			if(t.indexOf(":")!=-1)
			{
			var n = t.split(":");
			$("#tblSelCar").append('<tr><td style="padding:0; border:none;width:200px; padding-left:20px;">'+n[0]+' '+ n[1]+'</td><td style="font-size:20px;padding:0; border:none;cursor:pointer"  onclick="delFileType(2,'+i+')">x</td></tr>');
			j++;
			}
		}
		$("#dirCars").html("—品牌车型&nbsp;("+j+")");
	}
}
function delFileType(t,i)
{
	$.get("?act=upload&op=removeFileDirType&id="+$("#id").val()+"&type="+t+"&tid="+i,function(d)
	{
		var j = eval("("+d+")");
		if(j.st=="ok")
		{
			 getDetailInfo();
		}
	})
}
 
function initDetailPage()
{

		$("#year").html("<option value='0'>选择</option>");
		for(i=2013;i<new Date().getFullYear()+2;i++)
		{
			$("#year").append("<option>"+i+"</option>");
		}
		
        initSelect("brand",$("#brand").attr("did"));
		initSelect("type",$("#type").attr("did"));
		//initSelect("style",$("#style").attr("did"));
		$("#style").html("<option value='0'>选择</option>");
		$("#car").html("<option value='0'>选择</option>");
		$("#brand").change(function(){
			$("#brandName").val($("#brand").find("option:selected").text());
		    initSelect("car",$("#brand").val());
			$("#carName").val("");
		});
		$("#car").change(function(){
			$("#carName").val($("#car").find("option:selected").text());
		});
		$("#style").change(function(){
			$("#styleName").val($("#style").find("option:selected").text());
			
		});
		$("#type").change(function(){
		    initSelect("style",$("#type").val());
			$("#typeName").val($("#type").find("option:selected").text());
			$("#styleName").val("");
		});
		$("#avaliDate").datepicker();

		if($("#year").attr("sel")=="0")
		{
			$("#year").val(new Date().getFullYear());
		}
		else
		{
			$("#year").val($("#year").attr("sel"));
		}
}
function initSelect(obj,id)
{
	$.get("?act="+$("#act").val()+"&op=ajaxType&id="+id,function(data){
		var json = eval("("+data+")");		   
		if(json.length>0)
		{
			selop = "<option value='0'>选择</option>";
			for(i=0;i<json.length;i++)
			{
				selop = selop+"<option value='"+json[i].id+"'>"+json[i].name+"</option>";
			}
			//alert(id);
			$("#"+obj).html(selop);
		}
		else
		{
		    $("#"+obj).html("<option value='0'>无选项 不用选择</option>");
		}
		//if($("#"+obj).attr("sel")!="")
		//{
			//$("#"+obj).val($("#"+obj).attr("sel"));
			//$("#"+obj).change();
		//}
		//if(obj=="brand")
		//{
		    //$("#"+obj).val("2");
			//$("#brandName").val($("#brand").find("option:selected").text());
			//initSelect("car",$("#brand").val());
		//}
	});
}
function saveResult(st,t)
{
	if(st==1)
	{
		if(t=="post")
		{
			alert("发布成功");
		}
		if(t=="unpost")
		{
			alert("下架成功");
			showDataPage();
		}
		else if(t=="save")
		{
			//alert("保存成功");
			headerShowTips("信息保存成功，现在是否立即选择文件加入待审批列表？",2);
		}
		//showDataPage();
	}
}
function saveTypeResult(st)
{
	if(st==1)
	{
		//alert("发布成功111");
		//showDataPage();
		//getDetailInfo();
		var isChange = false;
		if($("#brandName").val()!="")
		{
			var isExist = false;
			var line = $("#brandName").val()+":"+$("#carName").val();
			for(i=0;i<garrc.length;i++)
			{
				if(garrc[i]==line)
				{
					isExist =true;
					break;
				}
			}
			if(!isExist)
			{
				garrc.push(line);
				isChange = true;
			}
		}
		if($("#typeName").val()!="")
		{
			var isExist = false;
			var line = $("#typeName").val()+":"+$("#styleName").val();
			for(i=0;i<garrt.length;i++)
			{
				if(garrt[i]==line)
				{
					isExist =true;
					break;
				}
			}
			if(!isExist)
			{
				garrt.push(line);
				isChange = true;
			}
		}
		if(isChange)
		{
			showFileTypes(garrt,garrc);
		}
	}
}
 function newUploader(button)
 {
 	var uploader = new plupload.Uploader({
	runtimes : 'html5,flash,silverlight,html4',
	browse_button : $("#"+button).find("#btnUploadPrevPic")[0], // you can pass in id...
	container: document.getElementById('users-contain'), // ... or DOM Element itself
	url : 'upload.php',
	flash_swf_url : 'js/Moxie.swf',
	silverlight_xap_url : 'js/Moxie.xap',
	chunk_size:"1mb",
	filters : {
		max_file_size : '2000mb',
		mime_types: [
			{title : "Image files", extensions : "jpg,gif,png"}
		]
	},

	init: {
		PostInit: function() {
			document.getElementById('filelist').innerHTML = '';

			//document.getElementById('uploadfiles').onclick = function() {
			//	uploader.start();
			//	return false;
			//};
		},

		FilesAdded: function(up, files) {
			plupload.each(files, function(file) {
				//document.getElementById(status).innerHTML += '<div id="' + file.id + '">' + file.name + ' (' + plupload.formatSize(file.size) + ') <b></b></div>';
				uploader.start();
				//document.getElementById('filelist').innerHTML += '<table width="100%" class="tblFile" id="tblFile_'+file.id +'"><tr><td width="10" rowspan="2">&nbsp;</td><td width="350">'+file.name+'</td><td width="100">'+plupload.formatSize(file.size)+'</td><td  width="223" rowspan="2">&nbsp;</td></tr><tr><td colspan="2" id="tdProess_'+file.id +'">&nbsp;</td></tr></table><br/>'
			});
		},

		UploadProgress: function(up, file) {
			//document.getElementById("tdProess_"+file.id).innerHTML = '<span>' + file.percent + "%</span>";
			//document.getElementById(file.id).getElementsByTagName('b')[0].innerHTML = '<span>' + file.percent + "%</span>";
			$("#"+button).find("p").html(file.percent+"%");
			if(file.percent==100)
			{
				$("#"+button).find("p").hide();
			}
			else
			{
				$("#"+button).find("p").show();
			}
		},
        FileUploaded:function(up,file,o)
		{
			var json = eval("("+o.response+")");
			//alert(json.path);
			//$("#"+button).css("background-image","url("+j.data.path.replace("s.","sb.")+")");
			//$("#"+button).find("img")[0].src = json.path;
			//$("#"+button).find("#imgPath").val(json.path);
			$.post("?act=upload&op=saveFileImg",{name:file.name,path:json.path,edit:gId},function(data){
				//alert(data);
				var j = eval("("+data+")");
				if(j.st=="ok")
				{
					$("#"+button).css("background-image","url("+j.path+")");
				}
				else
				{
					alert("处理失败，稍候重试");
				}
				});
		},
		UploadComplete:function(up,o)
		{
			 
		},
		Error: function(up, err) {
			document.getElementById('console').innerHTML += "\nError #" + err.code + ": " + err.message;
		}
	}
});
 uploader.init();
 return uploader;
}